package com.gewu.pm.ui.fragment.clockin;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.TitleBarFragment;
import com.gewu.pm.bean.AttendanceRecordChildBean;
import com.gewu.pm.bean.AttendanceRecordGroupBean;
import com.gewu.pm.ui.activity.clockin.ClockInDetailActivity;
import com.gewu.pm.ui.activity.clockin.ClockInRecordActivity;
import com.gewu.pm.ui.activity.filter.FilterClockInRecordSubordinateActivity;
import com.gewu.pm.ui.adapter.SubordinateAttendanceAdapter;
import com.gewu.pm.ui.adapter.SubordinateAttendanceChildAdapter;
import com.gewu.pm.ui.dialog.DateDialog;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 下属打卡记录Fragment
 */
public final class SubordinateClockInFragment extends TitleBarFragment<ClockInRecordActivity>
        implements BaseAdapter.OnItemClickListener, SubordinateAttendanceChildAdapter.OnItemActionListener {

    private TextView tvFilterType;
    private TextView tvDateRange;
    private ImageView ivFilter;
    private SmartRefreshLayout srlRefresh;
    private RecyclerView rvList;

    private SubordinateAttendanceAdapter mAdapter;
    private SelectTimePopup.Builder mFilterPopup;
    private int mCurrentFilterType = 0; // 0=全部，1=迟到，2=早退，3=未打卡

    public static SubordinateClockInFragment newInstance() {
        return new SubordinateClockInFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.subordinate_clock_in_fragment;
    }

    @Override
    protected void initView() {
        tvFilterType = findViewById(R.id.tv_filter_type);
        tvDateRange = findViewById(R.id.tv_date_range);
        ivFilter = findViewById(R.id.iv_filter);
        srlRefresh = findViewById(R.id.srl_refresh);
        rvList = findViewById(R.id.rv_list);

        mAdapter = new SubordinateAttendanceAdapter(getAttachActivity());
        // ⚠️ 必须在 setAdapter 之前设置监听器
        mAdapter.setOnChildItemClickListener(this); // 子Item整体点击
        mAdapter.setOnChildItemActionListener(this); // 子Item按钮点击
        rvList.setAdapter(mAdapter);

        srlRefresh.setOnRefreshListener(refreshLayout -> {
            loadData();
            refreshLayout.finishRefresh(1000);
        });

        setOnClickListener(tvFilterType, tvDateRange, ivFilter);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        // 模拟数据 - 使用分组结构
        List<AttendanceRecordGroupBean> groupList = new ArrayList<>();
        
        // 日期1：2025-01-01 - 4条记录
        AttendanceRecordGroupBean group1 = new AttendanceRecordGroupBean();
        group1.setId("group1");
        group1.setDate("2025-01-01");
        
        List<AttendanceRecordChildBean> childList1 = new ArrayList<>();
        
        // 第1条：张三 - 迟到
        AttendanceRecordChildBean child1 = new AttendanceRecordChildBean();
        child1.setId("1");
        child1.setUserName("张三");
        child1.setSignInTime("07:00:01");
        child1.setSignOutTime("16:30:01");
        child1.setCheckStatus(2); // 迟到
        child1.setSignOffStatus(1); // 正常签退
        childList1.add(child1);
        
        // 第2条：张三 - 正常
        AttendanceRecordChildBean child2 = new AttendanceRecordChildBean();
        child2.setId("2");
        child2.setUserName("张三");
        child2.setSignInTime("06:50:00");
        child2.setSignOutTime("18:00:00");
        child2.setCheckStatus(1); // 正常签到
        child2.setSignOffStatus(1); // 正常签退
        childList1.add(child2);
        
        // 第3条：李四 - 未打卡
        AttendanceRecordChildBean child3 = new AttendanceRecordChildBean();
        child3.setId("3");
        child3.setUserName("李四");
        child3.setSignInTime("");
        child3.setSignOutTime("16:30:01");
        child3.setCheckStatus(0); // 未打卡
        child3.setSignOffStatus(1); // 正常签退
        childList1.add(child3);
        
        // 第4条：王五 - 早退
        AttendanceRecordChildBean child4 = new AttendanceRecordChildBean();
        child4.setId("4");
        child4.setUserName("王五");
        child4.setSignInTime("06:55:00");
        child4.setSignOutTime("16:20:00");
        child4.setCheckStatus(1); // 正常签到
        child4.setSignOffStatus(2); // 早退
        childList1.add(child4);
        
        group1.setChildList(childList1);
        groupList.add(group1);
        
        // 日期2：2025-01-02 - 3条记录
        AttendanceRecordGroupBean group2 = new AttendanceRecordGroupBean();
        group2.setId("group2");
        group2.setDate("2025-01-02");
        
        List<AttendanceRecordChildBean> childList2 = new ArrayList<>();
        
        // 第5条：赵六 - 正常
        AttendanceRecordChildBean child5 = new AttendanceRecordChildBean();
        child5.setId("5");
        child5.setUserName("赵六");
        child5.setSignInTime("06:58:00");
        child5.setSignOutTime("17:30:00");
        child5.setCheckStatus(1); // 正常签到
        child5.setSignOffStatus(1); // 正常签退
        childList2.add(child5);
        
        // 第6条：孙七 - 迟到
        AttendanceRecordChildBean child6 = new AttendanceRecordChildBean();
        child6.setId("6");
        child6.setUserName("孙七");
        child6.setSignInTime("07:05:00");
        child6.setSignOutTime("17:00:00");
        child6.setCheckStatus(2); // 迟到
        child6.setSignOffStatus(1); // 正常签退
        childList2.add(child6);
        
        // 第7条：周八 - 未打卡签退
        AttendanceRecordChildBean child7 = new AttendanceRecordChildBean();
        child7.setId("7");
        child7.setUserName("周八");
        child7.setSignInTime("06:52:00");
        child7.setSignOutTime("");
        child7.setCheckStatus(1); // 正常签到
        child7.setSignOffStatus(0); // 未打卡签退
        childList2.add(child7);
        
        group2.setChildList(childList2);
        groupList.add(group2);
        
        mAdapter.setData(groupList);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_filter_type) {
            showFilterPopup();
        } else if (viewId == R.id.tv_date_range) {//日期范围选择
            new DateDialog.Builder(getAttachActivity())
                    .setTitle(getString(R.string.date_title))
                    // 确定按钮文本
                    .setConfirm(getString(R.string.common_confirm))
                    // 设置 null 表示不显示取消按钮
                    .setCancel(getString(R.string.common_cancel))
                    .setListener(new DateDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, int year, int month, int day) {
                            String time = year + getString(R.string.common_year) + month + getString(R.string.common_month) + day + getString(R.string.common_day);
                            toast(time);
                            tvDateRange.setText(time);
                        }
                    })
                    .show();
        } else if (viewId == R.id.iv_filter) {
            FilterClockInRecordSubordinateActivity.start(getAttachActivity());
        }
    }

    /**
     * 显示筛选弹窗
     */
    private void showFilterPopup() {
        if (mFilterPopup == null) {
            List<String> filterList = Arrays.asList(
                    getString(R.string.all),
                    getString(R.string.late),
                    getString(R.string.early_leave),
                    getString(R.string.not_clocked_in)
            );
            
            mFilterPopup = new SelectTimePopup.Builder(getAttachActivity())
                    .setList(filterList)
                    .setListener((popup, data) -> {
                        tvFilterType.setText(data);
                        // TODO: 根据筛选类型重新加载数据
                        toast("选择了：" + data);
                    });
        }
        mFilterPopup.showAsDropDown(tvFilterType);
    }

    /**
     * {@link BaseAdapter.OnItemClickListener}
     * 子列表Item点击事件
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        // 通过ViewHolder获取Adapter
        if (recyclerView.getAdapter() instanceof SubordinateAttendanceChildAdapter) {
            SubordinateAttendanceChildAdapter adapter = (SubordinateAttendanceChildAdapter) recyclerView.getAdapter();
            AttendanceRecordChildBean bean = adapter.getItem(position);
            // 跳转到详情页，传递id
            ClockInDetailActivity.start(getAttachActivity(), bean.getId());
        }
    }

    /**
     * {@link SubordinateAttendanceChildAdapter.OnItemActionListener}
     * 查看说明
     * @param isSignIn true=签到，false=签退
     */
    @Override
    public void onViewDescription(int position, AttendanceRecordChildBean bean, boolean isSignIn) {
        toast("查看填写说明");
        // TODO: 显示说明内容
    }

    /**
     * 查看图片
     * @param isSignIn true=签到，false=签退
     */
    @Override
    public void onViewPhoto(int position, AttendanceRecordChildBean bean, boolean isSignIn) {
        toast("查看图片");
        // TODO: 跳转到图片预览页面
    }
}
